package com.glory.gloryquickui.view

import android.app.Activity
import android.content.Context
import android.view.View
import android.widget.TextView
import com.glory.gloryquickui.R
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.utils.Utils
import com.glory.gloryquickui.viewmodel.QuickUIViewModel
import com.glory.gloryquickui.widget.datepicker.CustomDateRangePicker
import com.glory.gloryquickui.widget.text.CustomDateTextView

/**
 * FileName: DateNodeChooseController
 * Author: XJH
 * Date: 2021/4/20 14:41
 * Description: 时间节点选择器
 * History:
 */
class DateRangeChooseController(context: Context?, val viewModel: QuickUIViewModel): ViewController<FieldBean?>(context) {
    private var tvDateName: TextView? = null
    private var llDate: CustomDateTextView? = null
    private var mDatePicker: CustomDateRangePicker? = null
    private var mStartDate: String? = null
    private var mEndDate: String? = null

    override fun resLayoutId(): Int {
        return R.layout.layout_date_range_choose
    }

    override fun onCreatedView(view: View?) {
        tvDateName = view?.findViewById(R.id.tvDateName)
        llDate = view?.findViewById(R.id.llDate)

    }

    override fun onBindView(data: FieldBean?) {
        tvDateName?.text = data?.config?.label
        llDate?.setMiddleHint(data?.rangeSeparator)
        llDate?.setStartHint(data?.startPlaceholder)
        llDate?.setEndHint(data?.endPlaceholder)
        initDatePicker()
        llDate?.setOnClickListener{
            mDatePicker?.show()
        }
    }

    /**
     * 初始化时间选择器
     */
    private fun initDatePicker() {
        mDatePicker =
            CustomDateRangePicker(
                context,
                { startDate, endDate ->
                    mStartDate = startDate
                    mEndDate = endDate
                    llDate?.setText(mStartDate, mEndDate)
                    val map = viewModel.pageData.value!!.toMutableMap()
                    map[data!!.vModel] = "$mStartDate;$mEndDate"
                    viewModel.pageData.value = map
                }, Utils.getWindowHeight(context as Activity) / 3 * 2
            )
    }

    override fun masterPart(): View? {
        return llDate
    }
}